--local TargetTable={}

local DrawLasers=CreateClientConVar( "chud_showaimvector", 0, true, false )
local DrawLPLaser=CreateClientConVar( "chud_showplayeraimvector", 0, true, false )
local ShowTargets=CreateClientConVar( "chud_targetgui", 0, true, false )
local NameDist=CreateClientConVar( "chud_namedistance", 20, true, false )
local alpha=CreateClientConVar( "chud_alpha", 100, true, false )
local laserPoints={}

function PreDrawPlayerAim()
	if DrawLasers:GetInt()==1 then
		for f, v in pairs(player.GetAll()) do
			if v:Alive() then
				laserPoints[f]={}
				local at=v:LookupAttachment("eyes")
				laserPoints[f].startPos=v:GetAttachment(at).Pos
				if v==LocalPlayer() and ValidEntity(LocalPlayer():GetViewModel()) then
					at=v:GetViewModel():LookupAttachment("muzzle")
				end
				local tr=v:GetEyeTraceNoCursor()
				v:SetColor(255, 255, 255, 255)
				if v:GetAttachment(at) and v:GetAttachment(at).Pos then
					laserPoints[f].startPos=v:GetAttachment(at).Pos
				end
				if at and v==LocalPlayer() and ValidEntity(v:GetViewModel()) and (v:GetViewModel():GetAttachment(at) and v:GetViewModel():GetAttachment(at).Pos)then
					laserPoints[f].startPos=v:GetViewModel():GetAttachment(at).Pos
				end
				laserPoints[f].endPos=tr.HitPos
				laserPoints[f].Player=v
				laserPoints[f].Entity=tr.Entity
				laserPoints[f].HitPos=tr.HitPos
				if tr.HitWorld then
					tr.Entity=nil
				end
				if ValidEntity(tr.Entity) and tr.Entity:IsPlayer() then
					tr.Entity:SetColor(255, 0, 0, 255)
					if tr.Entity==LocalPlayer() then
						draw.SimpleTextOutlined( v:Nick().." is targeting you!", "ScoreboardText", ScrW()/2, ScrH()/4, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,150) )
					end
				end
			end
		end
	end
end
hook.Add("PreDrawTranslucentRenderables", "PreDrawPlayerAim", PreDrawPlayerAim)

--Enemy aim
local Laser = Material( "cable/redlaser" )
function DrawPlayerAim()
	render.SetMaterial( Laser )
	for f, v in pairs(laserPoints) do
		if (v.Player!=LocalPlayer() or DrawLPLaser:GetInt()==1) then
			render.StartBeam( 2 );
				render.AddBeam(
					v.startPos,
					5,
					CurTime(),
					Color( 255, 255, 255, 255 )
				);
				render.AddBeam(
					v.endPos,
					5,
					CurTime(),
					Color( 255, 255, 255, 255 )
				);
			render.EndBeam()
		end
	end
end
hook.Add("PostDrawTranslucentRenderables", "DrawPlayerAim", DrawPlayerAim)

local width=300
local border=2
function DrawTargeted()
	local dist=NameDist:GetInt()
	if ShowTargets:GetInt()==1 then
	--	MsgN("Running DrawTargeted()")
		draw.RoundedBox( 4, ScrW()-(width+20+(border)), 8, width+border*2, table.Count(player.GetAll())*dist+border*2+35, Color( 0, 0, 0, 150*(alpha:GetInt()/100) ) )
		draw.RoundedBox( 4, ScrW()-(width+20), 10, width, table.Count(player.GetAll())*dist+35, Color( 255, 255, 255, 200*(alpha:GetInt()/100) ) )
		draw.RoundedBox( 4, ScrW()-(width+20+(border)), 8, width+border*2, border*2+25, Color( 0, 0, 0, 255*(alpha:GetInt()/100) ) )
		draw.RoundedBox( 4, ScrW()-(width+20), 10, width, 25, Color( 200, 200, 200, 255*(alpha:GetInt()/100) ) )
	--	draw.DrawText("Shadowndacorner's TargetBoard", "ScoreboardText", ScrW()-(width+20), 15, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT)
		draw.SimpleTextOutlined( "Shadowndacorner's TargetBoard", "ScoreboardText", ScrW()-(width+15), 15, Color(255, 255, 255, 255*(alpha:GetInt()/100)), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 2, Color(0,0,0,150*(alpha:GetInt()/100)) )
		for f, tr in pairs(laserPoints) do
			local v=tr.Player
			--PrintTable(ents.FindInSphere(tr.HitPos, 1000))
			local txt=tostring(f)..". "..v:Nick()..": "
			local trgt="None"
			if !tr.Entity:IsPlayer() then
				draw.SimpleTextOutlined(txt..trgt, "ScoreboardText", ScrW()-(width+10), 20+f*dist, Color(255, 255, 255, 255*(alpha:GetInt()/100)), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 2, Color(0,0,0,50*(alpha:GetInt()/100)) )
			else
				trgt=tr.Entity:Nick()
				if tr.Entity==LocalPlayer() then
					draw.SimpleTextOutlined(txt..trgt, "ScoreboardText", ScrW()-(width+10), 20+f*dist, Color(255, 0, 0, 255*(alpha:GetInt()/100)), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 2, Color(0,0,0,50*(alpha:GetInt()/100)) )
				else
					draw.SimpleTextOutlined(txt..trgt, "ScoreboardText", ScrW()-(width+10), 20+f*dist, Color(0, 155, 255, 255*(alpha:GetInt()/100)), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 2, Color(0,0,0,50*(alpha:GetInt()/100)) )
				end
			end
		end
	end
end
hook.Add("HUDPaint", "DrawTargetedBy", DrawTargeted)

local function BuildMenu(CPanel)
    CPanel:AddControl( "Header", { Text = "#Combat Hud" }  )

	CPanel:AddControl( "Slider", { Label = "#Name Distance", Type = "Float", Command = "chud_namedistance", Min = "20", Max = "100" }  )
	CPanel:AddControl( "Slider", { Label = "#Alpha", Type = "Float", Command = "chud_alpha", Min = "0", Max = "100" }  )
    CPanel:AddControl( "CheckBox", { Label = "#Show TargetBoard",         Command = "chud_targetgui" }  )
    CPanel:AddControl( "CheckBox", { Label = "#Show Player Aim",     Command = "chud_showaimvector" }  )
    CPanel:AddControl( "CheckBox", { Label = "#Show Local Player Aim",     Command = "chud_showplayeraimvector" }  )
end

local function ShowCHudMenu(tog)
	CombatHudPanel = vgui.Create( "DFrame" )
	if tog then
		CombatHudPanel:SetPos(-210, 50)
	else
		CombatHudPanel:SetPos( 50,50 )
	end
	CombatHudPanel:SetSize( 210, 230 )
	CombatHudPanel:SetTitle( "Combat HUD Menu" )
	CombatHudPanel:SetVisible( true )
	CombatHudPanel:SetDraggable( true )
	CombatHudPanel:ShowCloseButton( true )
	CombatHudPanel:MakePopup()
	 
	DermaList = vgui.Create( "DPanelList", CombatHudPanel )
	DermaList:SetPos( 5,25 )
	DermaList:SetSize( 200, 200 )
	DermaList:SetSpacing( 5 ) -- Spacing between items
	DermaList:EnableHorizontal( false ) -- Only vertical items
	DermaList:EnableVerticalScrollbar( true ) -- Allow scrollbar if you exceed the Y axis
	 
	 		local CategoryContentSix = vgui.Create( "DNumSlider" )
		CategoryContentSix:SetSize( 150, 50 ) -- Keep the second number at 50
		CategoryContentSix:SetText( "Name Distance" )
		CategoryContentSix:SetMin( 20 )
		CategoryContentSix:SetMax( 100 )
		CategoryContentSix:SetDecimals( 0 )
		CategoryContentSix:SetConVar( "chud_namedistance" )
	DermaList:AddItem( CategoryContentSix ) -- Add the item above
	
			local CategoryContentSeven = vgui.Create( "DNumSlider" )
		CategoryContentSeven:SetSize( 150, 50 ) -- Keep the second number at 50
		CategoryContentSeven:SetText( "TargetBoard Alpha" )
		CategoryContentSeven:SetMin( 0 )
		CategoryContentSeven:SetMax( 100 )
		CategoryContentSeven:SetDecimals( 0 )
		CategoryContentSeven:SetConVar( "chud_alpha" )
	DermaList:AddItem( CategoryContentSeven ) -- Add the item above
	 
		local CategoryContentOne = vgui.Create( "DCheckBoxLabel" )
		CategoryContentOne:SetText( "Show TargetBoard" )
		CategoryContentOne:SetConVar( "chud_targetgui" )
		CategoryContentOne:SetValue( ShowTargets:GetInt() )
		CategoryContentOne:SizeToContents()
	DermaList:AddItem( CategoryContentOne ) -- Add the item above
	 
		local CategoryContentTwo = vgui.Create( "DCheckBoxLabel" )
		CategoryContentTwo:SetText( "Show Player Aim" )
		CategoryContentTwo:SetConVar( "chud_showaimvector" )
		CategoryContentTwo:SetValue( DrawLasers:GetInt() )
		CategoryContentTwo:SizeToContents()
	DermaList:AddItem( CategoryContentTwo ) -- Add the item above
	 
		local CategoryContentThree = vgui.Create( "DCheckBoxLabel" )
		CategoryContentThree:SetText( "Show Local Player Aim" )
		CategoryContentThree:SetConVar( "chud_showplayeraimvector" )
		CategoryContentThree:SetValue( DrawLPLaser:GetInt() )
		CategoryContentThree:SizeToContents()
	DermaList:AddItem( CategoryContentThree ) -- Add the item above
end
concommand.Add("chud_menu", ShowCHudMenu)
concommand.Add("+chud_menu", function()
	ShowCHudMenu(true)
	local pressTime=CurTime()
	hook.Add("HUDPaint", "BringCHudMenuIn", function()
		local frac=math.Clamp((CurTime()-pressTime)/(0.1), 0, 1)
		CombatHudPanel:SetPos(Lerp(frac, -210, 25), 50)
		if frac>=1 then
			hook.Remove("HUDPaint", "BringCHudMenuIn")
		end
	end)
end)
concommand.Add("-chud_menu", function()
	local pressTime=CurTime()
	gui.EnableScreenClicker(false)
	hook.Add("HUDPaint", "BringCHudMenuIn", function()
		local frac=math.Clamp((CurTime()-pressTime)/(0.1), 0, 1)
		CombatHudPanel:SetPos(Lerp(frac, 25, -210), 50)
		if frac>=1 then
			hook.Remove("HUDPaint", "BringCHudMenuIn")
			CombatHudPanel:SetVisible(false)
		end
	end)
end)

local function MenuStuff()
	spawnmenu.AddToolMenuOption("Options", "Shadowndacorner", "Combat Hud", "#Combat Hud", "", "", BuildMenu)
end
hook.Add("PopulateToolMenu", "AddSNDCMenuStuff", MenuStuff)

local function CreateCategories()
	spawnmenu.AddToolCategory("Options", "Shadowndacorner", "#Shadowndacorner")
end
hook.Add("AddToolMenuCategories", "CreateShadOptions", CreateCategories)